package com.zhixin.fans.ks.ui.main.index;


import android.app.Fragment;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.zhixin.fans.ks.KeyConstant;
import com.zhixin.fans.ks.R;
import com.zhixin.fans.ks.api.CommonApi;
import com.zhixin.fans.ks.api.res.IndexModule;
import com.zhixin.fans.ks.api.res.SubModule;
import com.zhixin.fans.ks.api.res.UserInfo;
import com.zhixin.fans.ks.manager.UserInfoManager;
import com.zhixin.fans.ks.ui.base.BaseDaggerFragment;
import com.zhixin.fans.ks.ui.charge.ChargeCoinActivity;
import com.zhixin.fans.ks.ui.charge.ModuleGoodActivity;
import com.zhixin.fans.ks.ui.user.UserDetailActivity;

import org.eenie.common.subscribes.ProgressSubscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class IndexV2Fragment extends BaseDaggerFragment implements BaseQuickAdapter.OnItemClickListener, Observer<UserInfo> {


    @BindView(R.id.img_user_header)
    ImageView mImgUserHeader;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;
    @BindView(R.id.tv_user_id)
    TextView mTvUserId;
    @BindView(R.id.tv_fb_count)
    TextView mTvFbCount;
    @BindView(R.id.btn_buy_fb)
    QMUIRoundButton mBtnBuyFb;
    @BindView(R.id.lay_user_info)
    RelativeLayout mLayUserInfo;
    @BindView(R.id.rv_module)
    RecyclerView mRvModule;
    Unbinder unbinder;


    BusyModuleAdapter mBusyModuleAdapter;

    //    CoupleApi mPosPaiApi;
    CommonApi mCommonApi;

    List<IndexModule> mIndexModules = new ArrayList<>();

    UserInfoManager mUserInfoManager;

    public IndexV2Fragment(String title) {
        super(title);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_index_v2;
    }

    @Override
    public void initData(Bundle bundle) {
        mCommonApi = getApplicationComponent()
                .getApiManager()
                .create(CommonApi.class);

        mUserInfoManager = getCoupleAppComponent().getUserInfoManager();

    }

    @Override
    public void initUI(View rootView) {
        unbinder = ButterKnife.bind(this, rootView);

        mRvModule.setLayoutManager(new LinearLayoutManager(mContext));
        mRvModule.setAdapter(mBusyModuleAdapter = new BusyModuleAdapter(mIndexModules));
        mBusyModuleAdapter.setOnItemClickListener(this);
        mBusyModuleAdapter.addHeaderView(LayoutInflater.from(mContext).inflate(R.layout.item_index_header_layout, null));
        mUserInfoManager.observe(this, this);
        mUserInfoManager.update();

        fetchModule();

    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }




    private void fetchModule() {
        mCommonApi.fetchModule()
                .subscribe(new ProgressSubscriber<List<IndexModule>>() {
                    @Override
                    public void onNext(List<IndexModule> indexModules) {
                        mIndexModules.clear();
                        mIndexModules.addAll(indexModules);
                        mBusyModuleAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        if (adapter.getItem(position) instanceof SubModule) {
            SubModule module = (SubModule) adapter.getItem(position);
            startActivity(new Intent(mContext, ModuleGoodActivity.class).putExtra(KeyConstant.KEY_SUB_MODULE, JSON.toJSONString(module)));

        }

//        IndexModule indexModule = (IndexModule) adapter.getItem(position);
//
//        startActivity(new Intent(mContext, ModuleItemActivity.class)
//                .putExtra("module_type", JSON.toJSONString(indexModule)));
    }


    @OnClick(R.id.btn_buy_fb)
    public void buyFb() {
        startActivity(new Intent(mContext, ChargeCoinActivity.class));
    }


    @OnClick(R.id.lay_user_info)
    public void userInfo() {
        startActivity(new Intent(mContext, UserDetailActivity.class));
    }

    @Override
    public void onChanged(@Nullable UserInfo userInfo) {
        if (userInfo != null && userInfo.getU_id() > 0) {
            Glide.with(mContext).asBitmap().load(userInfo.getU_headPicture()).into(mImgUserHeader);
            mTvUserName.setText(userInfo.getU_nickName());
            mTvUserId.setText(String.format("ID：%s", userInfo.getU_id()));
            mTvFbCount.setText(String.format("粉币：%s", userInfo.getU_usermoney()));
            mLayUserInfo.setVisibility(View.VISIBLE);
        } else {
            Glide.with(mContext).asBitmap().load(R.mipmap.ic_model_qq).into(mImgUserHeader);
            mTvUserName.setText("未登录");
            mTvUserId.setText("ID:-------");
            mLayUserInfo.setVisibility(View.VISIBLE);
        }
    }
}
